package com.smart.primary.sys.service.impl;



import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.DateUtil;
import com.smart.primary.common.util.Util;
import com.smart.primary.sys.dao.OrgMapper;
import com.smart.primary.sys.service.OrgService;
import com.smart.primary.sys.vo.Org;
import com.smart.primary.sys.vo.TreeNode;
import com.smart.primary.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class OrgServiceImpl implements OrgService {

    @Resource
    private OrgMapper sysOrgMapper;

    public OrgServiceImpl() {
    }

    public Org queryObject(Org sysOrg) throws Exception {
        return (Org)this.sysOrgMapper.queryObject(sysOrg);
    }

    public List<Org> queryList(Org sysOrg) throws Exception {
        return this.sysOrgMapper.queryList(sysOrg);
    }

    public List<Org> queryListByPage(Org sysOrg) throws Exception {
        return this.sysOrgMapper.queryListByPage(sysOrg);
    }

    public Integer queryCount(Org sysOrg) throws Exception {
        Integer ct = this.sysOrgMapper.queryCount(sysOrg);
        return ct == null ? 0 : ct;
    }

    public String getMaxOrgNo(Org sysOrg) {
        String max = this.sysOrgMapper.getMaxOrgNo(sysOrg);
        return max;
    }

    public AjaxResult saveOrUpdate(Org sysOrg, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orgNo = sysOrg.getOrgNo();
        int count = this.sysOrgMapper.queryCount(sysOrg);
        if (count > 0) {
            return AjaxResult.failure("机构名称已经存在!");
        } else {
            String userId = SessionUtil.getUserId(request,response);
            Date nowTime = DateUtil.changeStrToDate(DateUtil.getNowTime());
            Org tempBean;
            int i;
            if (orgNo != null && !orgNo.equals("")) {
                tempBean = (Org)this.sysOrgMapper.queryObject(sysOrg);
                int isSystem = tempBean.getIsSystem();
                if (1 == isSystem) {
                    return AjaxResult.failure("系统数据禁止修改");
                } else {
                    sysOrg.setUpdateUserId(userId);
                    sysOrg.setUpdateTime(nowTime);
                    i = this.sysOrgMapper.update(sysOrg);
                    return i > 0 ? AjaxResult.updateSuccess() : AjaxResult.failure("信息修改失败!");
                }
            } else {
                tempBean = new Org();
                tempBean.setOrgNo(sysOrg.getParentNo());
                Org pBean = (Org)this.sysOrgMapper.queryObject(tempBean);
                i = pBean.getIsLeaf();
                if (i == 1) {
                    tempBean.setIsLeaf(0);
                    this.sysOrgMapper.update(tempBean);
                }

                String parentNo = sysOrg.getParentNo();
                String maxOrgNo = this.sysOrgMapper.getMaxOrgNo(sysOrg);
                orgNo = Util.getOrgNo(parentNo, maxOrgNo);
                sysOrg.setOrgNo(orgNo);
                sysOrg.setIsLeaf(1);
                sysOrg.setCreateUserId(userId);
                sysOrg.setCreateTime(nowTime);
                sysOrg.setUpdateUserId(userId);
                sysOrg.setUpdateTime(nowTime);
                int aa = this.sysOrgMapper.insert(sysOrg);
                return aa > 0 ? AjaxResult.saveSuccess() : AjaxResult.failure("信息添加失败!");
            }
        }
    }

    public Integer insert(Org sysOrg) throws Exception {
        return this.sysOrgMapper.insert(sysOrg);
    }

    public Integer update(Org sysOrg) throws Exception {
        return this.sysOrgMapper.update(sysOrg);
    }

    public Integer delete(String orgNos) throws Exception {
        if (orgNos != null && !orgNos.equals("")) {
            String[] orgNoArr = orgNos.split(",");
            String[] var3 = orgNoArr;
            int var4 = orgNoArr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String orgNo = var3[var5];
                Org sysOrg = new Org();
                sysOrg.setOrgNo(orgNo);
                Org tempBean = (Org)this.sysOrgMapper.queryObject(sysOrg);
                List<Org> list = this.sysOrgMapper.queryList(sysOrg);
                if (list == null || list.size() == 0) {
                    this.sysOrgMapper.delete(sysOrg.getOrgNo());
                    Org bean = new Org();
                    bean.setOrgNo(tempBean.getParentNo());
                    List<Org> tempList = this.sysOrgMapper.queryList(bean);
                    if (tempList == null || tempList.size() == 0) {
                        bean.setIsLeaf(1);
                        this.sysOrgMapper.update(bean);
                    }
                }
            }
        }

        return 1;
    }

    public List<Object> getOrgListTree(Org sysOrg) throws Exception {
        sysOrg.setPageSize(9999);
        List<Org> list = this.sysOrgMapper.queryListByPage(sysOrg);
        List<Object> resList = new ArrayList();
        if (list != null && list.size() > 0) {
            Iterator var4 = list.iterator();

            while(var4.hasNext()) {
                Org bean = (Org)var4.next();
                String orgNo = bean.getOrgNo();
                String parentNo = bean.getParentNo();
                String name = bean.getOrgName();
                TreeNode node = new TreeNode();
                node.setId(orgNo);
                node.setParentId(parentNo);
                node.setName(name);
                node.setNocheck(false);
                Map<String, String> attrs = new HashMap();
                if ("-1".equals(parentNo)) {
                    attrs.put("rootFlag", "1");
                } else {
                    attrs.put("rootFlag", "0");
                }

                node.setAttrs(attrs);
                node.setParent(false);
                resList.add(node);
            }
        }

        return resList;
    }

    public List<Org> getListByPid(Org sysOrg) throws Exception {
        List<Org> list = this.sysOrgMapper.queryListByPage(sysOrg);
        if (list == null || ((List)list).size() == 0) {
            sysOrg = (Org)this.sysOrgMapper.queryObject(sysOrg);
            list = new ArrayList();
            ((List)list).add(sysOrg);
        }

        return (List)list;
    }
}